package com.activemanager.dashboard;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.activemanager.digitalTrain.WelcomeCalibrating_DT0;
import com.activemanager.healthGadget.FootReader;
import com.activemanager.myHealthProfile.Q1Activity;
import com.activemanager.myHealthProfile.Q1MainActivity;
import com.activemanager.workoutAssistant.WorkoutAssistant_W1;
import com.activemanager.workoutAssistant.WorkoutAssistant_W2;
import com.activemanager.workoutAssistant.WorkoutAssistant_W3;
import com.example.android.navigationdrawerexample.R;
import com.example.android.navigationdrawerexample.R.drawable;
import com.example.android.navigationdrawerexample.R.id;
import com.example.android.navigationdrawerexample.R.layout;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class H0Fragment extends Fragment implements OnTouchListener,
android.view.GestureDetector.OnGestureListener {
	
	ImageView workout = null;
	ImageView digital = null;
	ImageView foot = null;
	MyView myview;
	GestureDetector mGestureDetector;
	private int verticalMinDistance = 10;
	private int minVelocity = 0;
	float x1 = 0;
	float y1 = 0;
	float x2 = 0;
	float y2 = 0;
	donghuaview dhview;
	LinearLayout donghuaLL;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		View rootView = inflater.inflate(R.layout.h0_fragment_layout, container, false);
		
		donghuaLL=(LinearLayout) rootView.findViewById(R.id.relativeLayout3);
		dhview=new donghuaview(getActivity());
		donghuaLL.addView(dhview);
		
		
		workout = (ImageView) rootView.findViewById(R.id.imageView11);
		digital = (ImageView) rootView.findViewById(R.id.imageView12);
		foot = (ImageView) rootView.findViewById(R.id.imageView21);
		final Intent intent = new Intent();
		workout.setOnClickListener(new View.OnClickListener() {
	
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				final Dialog dialog = new Dialog(getActivity(),
						R.style.MyDialog);
				final BitmapFactory.Options opt = new BitmapFactory.Options();
				opt.inPreferredConfig = Bitmap.Config.RGB_565;
				opt.inPurgeable = true;
				opt.inInputShareable = true;

				dialog.setContentView(R.layout.p30);
				final LinearLayout img = (LinearLayout) dialog
						.findViewById(R.id.linearLayout);
//				InputStream is = getResources().openRawResource(R.drawable.u165_normal);
//				Bitmap bit = BitmapFactory.decodeStream(is, null, opt);
//				img.setBackgroundDrawable(new BitmapDrawable(bit));
				img.setBackgroundResource(R.drawable.bg_for_popup_54);

				dialog.show();
				Window dialogWindow = dialog.getWindow();
				WindowManager m = getActivity().getWindowManager();
				Display d = m.getDefaultDisplay();
				WindowManager.LayoutParams p = dialogWindow.getAttributes();
				p.height = (int) (d.getHeight() * 0.8);
				p.width = (int) (d.getWidth() * 0.9);
				dialogWindow.setAttributes(p);

				String[] mFrom = new String[] { "profile_img", "profile_item" };
				String[] profileTitle = { "Jogging", "Badminton", "Badminton PRO",
						"Cycling", "Walking", "Dance" };
				int[] mTo = new int[] { R.id.profile_img, R.id.profile_item };
				List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
				Map<String, Object> mMap = null;
				for (int i = 0; i < profileTitle.length; i++) {
					mMap = new HashMap<String, Object>();
					int drawable = getResources().getIdentifier(
							"choose_u" + (155 + i) + "_normal", "drawable",
							getActivity().getPackageName());
					mMap.put("profile_img", drawable);
					mMap.put("profile_item", profileTitle[i]);
					mList.add(mMap);
				}
				final ListView mListView = (ListView) dialog
						.findViewById(R.id.list_view);
				SimpleAdapter mAdapter = new SimpleAdapter(getActivity(),
						mList, R.layout.drawer_list_item_for_sports, mFrom, mTo);
				mListView.setAdapter(mAdapter);
				mListView.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
							long arg3) {
						// TODO Auto-generated method stub
						Intent intent = new Intent();
						if (pos == 0) {
							intent.setClass(getActivity(),
									WorkoutAssistant_W1.class);
							startActivity(intent);
							dialog.dismiss();
						} else if (pos == 1) {
							intent.setClass(getActivity(),
									WorkoutAssistant_W2.class);
							startActivity(intent);
							dialog.dismiss();
						}else if (pos == 2) {
							intent.setClass(getActivity(),
									WorkoutAssistant_W3.class);
							startActivity(intent);
							dialog.dismiss();
						}
					}
				});
				
				Button btn_cancel = (Button) dialog
						.findViewById(R.id.button1);
				btn_cancel.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
				
			}

		});
		digital.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				intent.setClass(getActivity(), WelcomeCalibrating_DT0.class);
				startActivity(intent);
			}

		});

			foot.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				intent.setClass(getActivity(), FootReader.class);
				startActivity(intent);
			}

		});
		
		initPic(rootView);
		LinearLayout LL = (LinearLayout) rootView.findViewById(R.id.LV);
		LL.setOnTouchListener(this);
		LL.setLongClickable(true);
		myview = new MyView(getActivity());
		LL.addView(myview);
		mGestureDetector = new GestureDetector(
				(android.view.GestureDetector.OnGestureListener) this);
		return rootView;
	}
	
	private void initPic(View rootView) {
		// TODO Auto-generated method stub
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;
		
		ImageView img = (ImageView) rootView.findViewById(R.id.imageView1);
		InputStream is = getActivity().getResources().openRawResource(
				R.drawable.star_shining);
		Bitmap bit = BitmapFactory.decodeStream(is, null, opt);
		img.setImageBitmap(bit);
		
		
		ImageView img1 = (ImageView) rootView.findViewById(R.id.imageView2);
		InputStream is1 = getActivity().getResources().openRawResource(
				R.drawable.star_shining_b);
		Bitmap bit1 = BitmapFactory.decodeStream(is1, null, opt);
		img1.setImageBitmap(bit1);
		
	
        ImageView img2 = (ImageView) rootView.findViewById(R.id.imageView11);
		InputStream is2 = getActivity().getResources().openRawResource(
				R.drawable.u75_normal);
		Bitmap bit2 = BitmapFactory.decodeStream(is2, null, opt);
		img2.setImageBitmap(bit2);	
		
		ImageView img3 = (ImageView) rootView.findViewById(R.id.imageView12);
		InputStream is3 = getActivity().getResources().openRawResource(
				R.drawable.u75_normal);
		Bitmap bit3 = BitmapFactory.decodeStream(is3, null, opt);
		img3.setImageBitmap(bit3);
		
		ImageView img4 = (ImageView) rootView.findViewById(R.id.imageView21);
		InputStream is4 = getActivity().getResources().openRawResource(
				R.drawable.u75_normal);
		Bitmap bit4 = BitmapFactory.decodeStream(is4, null, opt);
		img4.setImageBitmap(bit4);
		
		ImageView img5 = (ImageView) rootView.findViewById(R.id.imageView22);
		InputStream is5 = getActivity().getResources().openRawResource(
				R.drawable.u75_normal);
		Bitmap bit5 = BitmapFactory.decodeStream(is5, null, opt);
		img5.setImageBitmap(bit5);
		
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		//������
		if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) { 
		     myview.left_or_right=1;
		     myview.left_dx=0;
			 myview.first_flag=false;
			 if(myview.testx[8]>myview.sidex){myview.scale_flag=(myview.test).length-1;}
			 else{
			 if(myview.scale_flag>1){
				 myview.scale_flag--;}	    
			  }}
	      //������
	   else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) { 
		   myview.left_or_right=2;
		   myview.right_dx=0;
		   myview.first_flag=false;
			 if(myview.scale_flag<((myview.test).length-1)){
	         myview.scale_flag++;}			  
				  
						 
			  }
       
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean onTouch(View arg0, MotionEvent arg1) {
		// TODO Auto-generated method stub
		return mGestureDetector.onTouchEvent(arg1);
		
	}
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	
}
